package model.admin;

import ecom.beans.EcomAdminRemote;
import ecom.shell.ShellRemote;
import java.util.Collection;
import java.util.Map;
import model.AbstractPage;

/**
 *
 * @author Sebastien
 */
public class StockManagement extends AbstractPage {

    private Collection stores;
    private Collection products;
    private int selectedStore;
    private int selectedProduct;
    private int quantity;
    private double price;
    private static final String xsl = "C://JAVA//wkspace//ecom//ecom-war//web//XSL/admin/stock.xsl";
    private static final String formPage = "admin/stock/form.jsp";
    private static final String listPage = "admin/list.jsp";

    public String add() throws Exception {
        Map map = getParameters();
        String[] requestAction = (String[]) map.get("action");
        EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");

        // add in database
        if (requestAction != null && "add".equalsIgnoreCase(requestAction[0])) {
            admin.createStockLine(selectedStore, selectedProduct, quantity, price);
            return list();
        } // set the form
        else {
            stores = admin.getStores();
            products = admin.getProducts();
            this.setPage(formPage);
        }
        return SUCCESS;
    }

    public String list() throws Exception {
        ShellRemote shell = (ShellRemote) getSession().get("shell");
        this.setDataToPrint(shell.executeCommand("product -price " + Integer.MAX_VALUE));
        this.setXslPath(xsl);
        this.setPage(listPage);
        return SUCCESS;
    }

    public String delete() throws Exception {
        String[] id_store = (String[]) getParameters().get("id_store");
        String[] id_product = (String[]) getParameters().get("id_product");
        String[] quantityToDelete = (String[]) getParameters().get("quantity");

        if (id_store != null && !"".equalsIgnoreCase(id_store[0]) &&
                id_product != null && !"".equalsIgnoreCase(id_product[0]) &&
                quantityToDelete != null && !"".equalsIgnoreCase(quantityToDelete[0])) {
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            admin.removeStockLine(Integer.valueOf(id_store[0]),
                                  Integer.valueOf(id_product[0]),
                                  Integer.valueOf(quantityToDelete[0]));
        }
        return list();
    }

    @Override
    public String execute() throws Exception {
        message = "StoreManagement()";
        return SUCCESS;
    }

    public Collection getProducts() {
        return products;
    }

    public Collection getStores() {
        return stores;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public void setSelectedProduct(int selectedProduct) {
        this.selectedProduct = selectedProduct;
    } 

    public void setSelectedStore(int selectedStore) {
        this.selectedStore = selectedStore;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}